<?php
defined('_JEXEC') or die();

class WrdControllerLnum extends JControllerLegacy 
{
        protected $option;
        protected $controller; 
		function __construct( $default = array()){
    		$this->option = 'com_wrd';
    		$this->controller = 'lnum';
			parent::__construct( $default );
    	}

      	function display()
 		{
			switch($this->getTask()){
				case 'add':{
						JRequest::setVar( 'hidemainmenu', 1 );
						JRequest::setVar( 'layout', 'form'  );
						JRequest::setVar( 'view'  , 'lnum');
					} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'lnum');
	            	break;
			}
			parent::display();
        }
        
  
    function save(){
       $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d");
       $model=$this->getModel('lnum');
	   //get date current
       $id_category=$model->store($post,'lnum');
       if($id_category!=0){
            $this->setRedirect('index.php?option=com_wrd&controller=lnum','Saved');
			return true;              
       }
       else{
            $this->setRedirect('index.php?option=com_wrd&controller=lnum','Error Saved');
            return false;  
       }
    }    
   
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('lnum');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=lnum' );
	}
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}
		$model = $this->getModel('lnum');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=lnum' );
	}

	function unpublish(){
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}
		$model = $this->getModel('lnum');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=lnum' );
	}
}